const http = require('../../../utils/http.js');
const app = getApp()
const utils = require('../../../utils/util.js')
Page({
  data: {
    hideBottom: true,
    swiperCurrent: 0,
    detail: {},
    Modelbottom: -1000,
    goodsNum: 1,
    id: '',
    titleHeight: '',
    skuList: [{
      children: [{
        name: '请选择规格',
        default: 1
      }]
    }],
    statusd:1
  },
  errorFunction(e) {
    console.log('加载图片失败', e)
    var _this = this,
      detail = _this.data.detail
    detail.small_images = ['https://huixuangou.oss-cn-beijing.aliyuncs.com/wechat/Xz_MiNi_image/detail360-360.png']
    _this.setData({
      detail: detail
    })
  },
  onLoad: function (options) {
    var _this = this
    // titleIcon
    var query = wx.createSelectorQuery()
    query.select('#titleIcon').boundingClientRect()
    query.exec(function (res) {
      console.log(res)
      _this.setData({
        titleHeight: res[0].height
      })
    })
    _this.setData({
      id: options.id
    })
    _this.onPageInfo()
  },
  onReady: function () {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  //切换选项卡  
  swiperChange: function (e) {
    var _this = this
    _this.setData({
      swiperCurrent: e.detail.current
    })
  },
  hidShowModals() {
    var _this = this,
      bottom = _this.data.Modelbottom
    _this.setData({
      Modelbottom: bottom == -1000 ? 0 : -1000
    })
  },
  showShowModals() {
    var _this = this,
      bottom = _this.data.Modelbottom
    if (_this.data.skuList[0].children[0].name != '请选择规格') {
      _this.setData({
        Modelbottom: bottom == -1000 ? 0 : -1000
      })
      return false
    }
    http.getRequest('v1/goodsSku', {
        goods_id: _this.data.id,
        attr_ids: _this.data.detail.attr_ids,
        
      },
      function (res) {
        console.log(res)
        _this.setData({
          Modelbottom: bottom == -1000 ? 0 : -1000,
          skuList: _this.data.skuList[0].children[0].name != '请选择规格' ? _this.data.skuList : res.data.goods_sku
        })
      },
      function (err) {})
  },
  onShowModalList() {
    var _this = this,
      bottom = _this.data.Modelbottom
    if (_this.data.skuList[0].children[0].name != '请选择规格') {
      var sku_ids = [],
        skulist = _this.data.skuList,
        skuid = ''
      for (let i in skulist) {
        for (let t in skulist[i].children) {
          if (skulist[i].children[t].default == 1) {
            sku_ids = sku_ids + skulist[i].children[t].attr_ids + ','
            skuid = skuid + skulist[i].children[t].mall_sku_id
          }
        }
      }
      sku_ids = sku_ids.substring(0, sku_ids.length - 1)
      console.log(sku_ids,skuid)
      wx.navigateTo({
        url: '/pages/onNewPages/selfOrderPay/selfOrderPay?goods_id=' + _this.data.id + '&sku_ids=' + sku_ids + '&buy_num=' + _this.data.goodsNum + '&skuid=' + skuid,
      })
    } else {
      http.getRequest('v1/goodsSku', {
          goods_id: _this.data.id,
          attr_ids: _this.data.detail.attr_ids
        },
        function (res) {
          console.log(res)
          _this.setData({
            Modelbottom: bottom == -1000 ? 0 : -1000,
            skuList: _this.data.skuList[0].children[0].name != '请选择规格' ? _this.data.skuList : res.data.goods_sku
          })
        },
        function (err) {})
    }
  },
  onPageInfo() {
    var _this = this
    http.getRequest('v1/goods/' + _this.data.id, {
        user_type: 3,
        id: _this.data.id
      },
      function (res) {
        console.log(res)
        res.data.small_images = res.data.small_images.split('|')
        _this.setData({
          detail: res.data,
          statusd:0
        })
      },
      function (err) {
        _this.setData({
          statusd:0
        })
      })
  },
  upDataNumber(e) {
    var _this = this,
      id = e.currentTarget.id,
      number = _this.data.goodsNum
    if (id == 1) {
      if (number == 1) {
        wx.showToast({
          title: '不能再减了',
          icon: 'none'
        })
        return false
      }
      _this.setData({
        goodsNum: number - 1
      })
    } else {
      _this.setData({
        goodsNum: number + 1
      })
    }
  },
  onGotoPayGoods() {
    var _this = this,
      sku_ids = [],
      skulist = _this.data.skuList,
      skuid = ''
    for (let i in skulist) {
      for (let t in skulist[i].children) {
        if (skulist[i].children[t].default == 1) {
          sku_ids = sku_ids + skulist[i].children[t].attr_ids + ','
          skuid = skuid + skulist[i].children[t].mall_sku_id
        }
      }
    }
    try {
      sku_ids = sku_ids.substring(0, sku_ids.length - 1)
    } catch (error) {
      wx.showToast({
        title: '请选择规格',
        icon:'none'
      })
      return false
    }
    console.log(sku_ids,skulist)
    wx.navigateTo({
      url: '/pages/onNewPages/selfOrderPay/selfOrderPay?goods_id=' + _this.data.id + '&sku_ids=' + sku_ids + '&buy_num=' + _this.data.goodsNum,
    })
    setTimeout(() => {
      _this.setData({
        Modelbottom: _this.data.bottom == -1000 ? 0 : -1000,
      })
    }, 500);
  },
  EnterShop() {
    wx.navigateBack({
      delta: 1
    })
  },
  onSkuTap(e) {
    var _this = this,
      id = e.currentTarget.id,
      list = _this.data.skuList
    for (let i in list) {
      for (let t in list[i].children) {
        if (e.currentTarget.dataset.findex == i) {
          list[i].children[t].default = 0
        }
      }
    }
    list[e.currentTarget.dataset.findex].children[e.currentTarget.dataset.index].default = 1
    _this.setData({
      skuList: list
    })
    _this.onOnlySukFunction()

  },
  /**
   * 获取单个规格内容
   */
  onOnlySukFunction() {
    var _this = this,
      sku_ids = [],
      skulist = _this.data.skuList,
      skuid = ''
    for (let i in skulist) {
      for (let t in skulist[i].children) {
        if (skulist[i].children[t].default == 1) {
          sku_ids = sku_ids + skulist[i].children[t].attr_ids + ','
          skuid = skuid + skulist[i].children[t].mall_sku_id
        }
      }
    }
    sku_ids = sku_ids.substring(0, sku_ids.length - 1)
    console.log(sku_ids)
    http.getRequest('v1/getSkuInfo', {
        goods_id: _this.data.id,
        sku_sttr: sku_ids
      },
      function (res) {
        console.log(res)
      },
      function (err) {})
  },
  shopImg() {
    var _this = this,
      detail = _this.data.detail
    detail.pict_url = 'https://huixuangou.oss-cn-beijing.aliyuncs.com/wechat/Xz_MiNi_image/detail360-360.png'
    _this.setData({
      detail: detail
    })
  }
})